export class Color {
  public r: number
  public g: number
  public b: number
  constructor(r: number, g: number, b: number) {
    this.r = r
    this.g = g
    this.b = b
  }
  /**
   *
   * @param hex #aabbcc
   * @returns
   */
  public static fromHex(hex: string): Color {
    const r = parseInt(hex.slice(1, 3), 16)
    const g = parseInt(hex.slice(3, 5), 16)
    const b = parseInt(hex.slice(5, 7), 16)
    return new Color(r, g, b)
  }

  public toHex(): string {
    return `#${this.r.toString(16).padStart(2, '0')}${this.g
      .toString(16)
      .padStart(2, '0')}${this.b.toString(16).padStart(2, '0')}`
  }

  /**
   *
   * @param color
   * @returns 0~441.6729559300637
   */
  getSimilarity(color: Color): number {
    const dr = this.r - color.r
    const dg = this.g - color.g
    const db = this.b - color.b
    // return dr * dr + dg * dg + db * db
    return Math.sqrt(dr * dr + dg * dg + db * db)
  }
}
